/*
 * @(#) Title.js
 *
 * <br> Copyright:  Copyright (c) 2017
 * <br>
 * <br> @author caijinhuang
 * <br> 2017-11-19 22:04:47
 */
import React from 'react';
import {UserUtil} from "../../UserUtil";
import {
    SiteHeader,
    NavItem,
    NavDropdown,
    MenuItem,
    NavSearch,
    Panel,
    Form,
    Modal,
    FormControl,
    Button,
    FormGroup,
    ControlLabel
} from '@share/shareui';

class Title extends React.Component {
    clocker = null;
    pathname = "";
    constructor(props){
        super(props);
        this.state={
            showModal:false,
            showManage:false,
            showRegist:false,
            isVailable:false,
            format:false,
            oldPW:"",//旧密码
            newPW:"",//新密码
            rePW:"",//重复密码

            params:{
                username:"",//账号
                password:"",//密码
                nickname:"",//姓名
                sex:"1",//性别
                department:"",//部门
                email:"",//邮箱
                phone:""//电话号码
            },

            allDepartment:[],//部门信息
            allGroup:[]//小组信息
        }

    }
    /*退出登陆*/
    logingOut(){
        let url = "/sharemeeting/login/loginOut.do";
        /*异步请求*/
        fetch(url,{
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            credentials: 'include',
        })
            .then((response) => response.json()) //把response转为json
            .then((responseData) => { // 上面的转好的json
                if(responseData.status === false){
                    alert(responseData.msg);
                    window.location.href="#/";
                }else{
                    window.location.href="#/";
                }
            }).catch((error)=> {
            window.location.href="#/";
        })
    }

    searchMeeting(title){
        this.props.renderList(title);
        this.clocker = null;
    }

    /*修改密码*/
    changePassWd(){
        if(this.state.newPW === this.state.rePW){
            let url = "/sharemeeting/user/changePassWd.do";
            /*异步请求*/
            fetch(url,{
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                credentials: 'include',
                xhrFields: {
                    withCredentials: true
                },
                body:JSON.stringify({oldPassword:this.state.oldPW,newPassword:this.state.newPW})
            })
                .then((response) => response.json()) //把response转为json
                .then((responseData) => { // 上面的转好的json
                    if(responseData.status === true){
                        this.close();
                        alert(responseData.msg+",请重新登陆");
                        window.location="#/";
                    }else{
                        alert(responseData.msg);
                    }
                }).catch((error)=> {
                alert('服务器出错了');
            })
        }else{
            alert("两次密码不一致，请重新输入");
        }
    }

    /*弹窗*/
    close(){this.setState({showModal: false,showManage:false,showRegist:false})};
    show(){this.setState({showModal: true})};
    showManage(){this.setState({showManage:true})};
    showRegist(){this.setState({showRegist:true})};

    render(){
        return (
            <div>
                <SiteHeader>
                    <SiteHeader.Left
                        logo={require('../logo.svg')}
                        logoAlt="这是logo"
                        title="会议纪要管理系统"
                        subTitle="v 1.0"
                        onClick={()=>{console.log("test");window.location.reload()}}
                    />
                    <SiteHeader.Right>
                        <SiteHeader.Nav>
                            {   window.location.hash === "#/index" ?
                                    <NavSearch placeholder="搜索会议标题" onChange={(e) => {
                                        /*延时加载*/
                                        var value = e.target.value;
                                        if (this.clocker === null) {
                                            this.clocker = setTimeout(() => {
                                                this.searchMeeting(value)
                                            }, 1000);
                                        } else {
                                            clearTimeout(this.clocker);//连续输入时重新计时，没有输入时，1秒后执行搜索函数。
                                            this.clocker = setTimeout(() => {
                                                this.searchMeeting(value)
                                            }, 1000);
                                        }
                                    }}/> : ""

                            }
                        </SiteHeader.Nav>
                        <SiteHeader.Nav>
                            <NavDropdown id="basic-nav-dropdown" icon="fa fa-user" title={UserUtil.userInfo.nickname}>
                                {/*/!*<MenuItem>管理中心</MenuItem>*/}
                                <MenuItem onClick={this.show.bind(this)}>修改密码</MenuItem>
                                <MenuItem onClick={this.logingOut.bind(this)}>退出</MenuItem>
                            </NavDropdown>
                        </SiteHeader.Nav>
                    </SiteHeader.Right>
                </SiteHeader>

                {/*修改密码弹出框*/}
                <Modal show={this.state.showModal} onHide={this.close.bind(this)}>
                    <Modal.Header closeButton>
                        <Modal.Title id="contained-modal-title-lg">密码修改</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>
                        <Panel.Body full>
                            <Form pageType="addPage">
                                <Form.Table>
                                    <Form.Tr>
                                        <Form.Label required>原始密码</Form.Label>
                                        <Form.Content>
                                            <FormControl type="password" onChange={(e)=>{
                                                this.state.oldPW = e.target.value;
                                            }}/>
                                        </Form.Content>
                                    </Form.Tr>
                                    <Form.Tr>
                                        <Form.Label required>新的密码</Form.Label>
                                        <Form.Content>
                                            <FormControl type="password" onChange={(e)=>{
                                                this.state.newPW = e.target.value;
                                                console.log(e.target.value);
                                            }}/>
                                        </Form.Content>
                                    </Form.Tr>
                                    <Form.Tr>
                                        <Form.Label required>重复密码</Form.Label>
                                        <Form.Content>
                                            <FormControl type="password" onChange={(e)=>{
                                                this.state.rePW = e.target.value;
                                                console.log(e.target.value);
                                            }}/>
                                        </Form.Content>
                                    </Form.Tr>
                                </Form.Table>
                            </Form>
                        </Panel.Body>
                    </Modal.Body>
                    <Modal.Footer>
                        <Button onClick={this.close.bind(this)}>取消</Button>
                        <Button onClick={this.changePassWd.bind(this)}>确定</Button>
                    </Modal.Footer>
                </Modal>
            </div>
        );
    }
}
export default Title


